var $ = require('common/helper.js');
//app.js
App({
	onLaunch: function () {
		var _this = this, person = $.storage('person');
		if (person && person.sign) {
			$.get('api.php?app=passport&act=check_sign',
				{ id: person.id, sign: person.sign }, function (json) {
					if ($.isJson(json.data)) {
						$.storage('person', json.data);
					} else {
						_this.setLogin();
					}
				});
		} else {
			_this.setLogin();
		}
	},
	setLogin: function () {
		wx.login({
			success: function (res) {
				if (res.code) {
					$.get('wx_interface.php?act=get_session_key',
						{ code: res.code }, function (json) {
							var openid = json.openid;
							wx.getSetting({ //获取用户的授权设置
								success: function (res) {
									if (res.authSetting['scope.userInfo'] ||
										res.errMsg == 'getSetting:ok') {
										wx.getUserInfo({ //已经授权,不会弹框
											lang: 'zh_CN',
											success: function (res) {
												var person = res.userInfo;
												person.openid = openid;
												$.post('api.php?app=core&act=weixin_auth', { userinfo: $.jsonString(person) }, function (json) {
													$.storage('person', json.data);
												});
											}
										});
									}
								}
							});
						});
				} else {
					console.log('获取用户登录状态失败！' + res.errMsg)
				}
			}
		});
	},
	globalData: {

	}
})